package br.jus.tjpe.negocio;

import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import br.jus.tjpe.dao.ParteDao;
import br.jus.tjpe.dominio.Parte;

@Stateless
public class ParteServicoImpl implements ParteServico {

	@EJB
	private ParteDao parteDao;

	/**
	 * @see br.jus.tjpe.negocio.ParteServico#consultarPartes()
	 */
	public List<Parte> consultarPartes() {
		return this.parteDao.buscarTodos();
	}

	/**
	 * @see br.jus.tjpe.negocio.ParteServico#consultarPartes(java.lang.String)
	 */
	public List<Parte> consultarPartes(String nome) {
		return this.parteDao.consultarPartes(nome);
	}

	/**
	 * @see br.jus.tjpe.negocio.ParteServico#salvar(br.jus.tjpe.dominio.Parte)
	 */
	public void salvar(Parte parte) {
		if (parte.getId() == null) {
			this.parteDao.inserir(parte);
		} else {
			this.parteDao.editar(parte);
		}
	}

	/**
	 * @see br.jus.tjpe.negocio.ParteServico#remover(br.jus.tjpe.dominio.Parte)
	 */
	public void remover(Parte parte) {
		this.parteDao.remover(parte);
	}
	
	@Resource(mappedName="jms/myCF")
	private ConnectionFactory connectionFactory;
	
	@Resource(mappedName="jms/myQueue")
	private Queue destination;
	
	public void enviarMensagem() {

		try {
			Connection connection = connectionFactory.createConnection();
			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);

			Message myMessage = session.createTextMessage();
			myMessage.setStringProperty("mensagem", "Testando JMS!");

			MessageProducer producer = session.createProducer(destination);
			producer.send(myMessage);
			System.out.println("Mensagem enviada!");

		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
